package cn.com.bsoft.cloud.service.serviceImpl;

import cn.com.bsoft.cloud.dao.cloud.ScheduleSiteDao;
import cn.com.bsoft.cloud.entity.ScheduleRecord;
import cn.com.bsoft.cloud.entity.ScheduleSite;
import cn.com.bsoft.cloud.entity.param.ScheduleParam;
import cn.com.bsoft.cloud.model.IMGetParameter;
import cn.com.bsoft.cloud.model.ScheduleRecordToken;
import cn.com.bsoft.cloud.model.ScheduleRecordTokenParameter;
import cn.com.bsoft.cloud.service.ScheduleAppointmentService;
import cn.com.bsoft.immunity.kotlin.JsonExtKt;
import cn.com.bsoft.immunity.utils.HttpUtilsKt;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import jdk.nashorn.internal.parser.JSONParser;
import kotlin.jvm.internal.Reflection;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

@Service
//@Transactional(value="txManagerCloud")
public class ScheduleAppointmentServiceImpl implements ScheduleAppointmentService {

    private static Logger logger= LogManager.getLogger(ScheduleAppointmentServiceImpl.class);

    @Autowired
    private ScheduleSiteDao scheduleSiteDao;

    @Override
    public List<ScheduleRecord> getScheduleRecordList(ScheduleParam map){
        List<ScheduleRecord> scheduleRecords =  new ArrayList<ScheduleRecord>();
//        String url ="http://172.16.5.35/pay/schedule/query2";
        String url ="http://172.16.5.35/test/schedule/query2";

        try {
            URL u1 = new URL(url);
            Object object = HttpUtilsKt.postJson(u1, map, JsonExtKt.getListJavaType(Reflection.getOrCreateKotlinClass(Object.class)));
            String json = JSON.toJSON(object).toString();
            List<ScheduleRecord> scheduleRecordList = JSONArray.parseArray(json, ScheduleRecord.class);
            if (scheduleRecordList !=null && scheduleRecordList.size() >0){
                List<String> ids = getAppointIdS(scheduleRecordList);
                List<ScheduleSite> getScheduleSites = scheduleSiteDao.getScheduleSite(ids);
                List<ScheduleSite> getScheduleStatusList = scheduleSiteDao.getScheduleStatus(ids);

                for (ScheduleRecord scheduleRecord:scheduleRecordList) {

                    if (getScheduleSites.size()>0){
                        for (ScheduleSite scheduleSite : getScheduleSites) {
                            if (scheduleRecord.getRecordID().equals(scheduleSite.getAppointmentID())){
                                scheduleRecord.setSiteAddress(scheduleSite.getSiteAddress());
                                scheduleRecord.setSiteCode(scheduleSite.getSiteCode());
                                scheduleRecord.setSiteName(scheduleSite.getSiteName());
                            }
                        }
                    }

                    if (getScheduleStatusList.size()>0){
                        for (ScheduleSite scheduleStatus : getScheduleStatusList) {
                            if (scheduleRecord.getRecordID().equals(scheduleStatus.getAppointmentID())){
                                scheduleRecord.setDeliveryStatus(scheduleStatus.getDeliveryStatus());
                            }
                        }
                    }
                    scheduleRecords.add(scheduleRecord);
                }
            }
        }catch (Exception e){
            logger.info(e.getMessage());
        }
        return scheduleRecords;
    }

    @Override
    public ScheduleRecordToken getUidForScheduleRecordId(ScheduleRecordTokenParameter map){
        ScheduleRecordToken scheduleRecordToken =  new ScheduleRecordToken();
        IMGetParameter imGetParameter = new IMGetParameter();
        imGetParameter.setAppID("mh.jieyi");
        imGetParameter.setPrincipal(map.getLoginName());
        String url ="https://i.mhwsw.com/im/get";

        try {
            URL imURL= new URL(url);
            Object object = HttpUtilsKt.postJson(imURL, imGetParameter, JsonExtKt.getJavaType(Reflection.getOrCreateKotlinClass(Object.class)));
            String json = JSON.toJSON(object).toString();
            scheduleRecordToken = JSON.parseObject(json, ScheduleRecordToken.class);
        }catch (Exception e){
            logger.info(e.getMessage());
        }

        return scheduleRecordToken;
    }

    public List<String> getAppointIdS(List<ScheduleRecord>  schedules){
        List<String> appIdList=new ArrayList<String>();
        for (ScheduleRecord s:schedules) {
            String recordID = s.getRecordID();
            appIdList.add(recordID);
        }
        return appIdList;
    }

}
